﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;

public partial class BasicBase_Vehicle_SelectDepartment : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            BindDepartmentList();
            BindAll();

            List<SlectDepartment> selectList = new List<SlectDepartment>();
            ViewState["selectList"] = selectList;
            BindSelect();
        }
    }


    protected void PermissionCategoryList_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindAll();
    }
    protected void ButtonAdd_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in AllList.Items)
        {
            if (Item.Selected)
            {
                int count = selectList.Where(or => or.value == Item.Value).Count();
                if (count == 0)
                {
                    selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
                }
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }
    protected void ButtonRemove_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in SelectList.Items)
        {
            if (Item.Selected)
            {
                if (selectList.Where(or => or.value == Item.Value).Count() > 0)
                {
                    selectList.Remove(selectList.Where(or => or.value == Item.Value).Single());
                }
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }

    void BindDepartmentList()
    {

        UserInfo userInfo = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name);
        DepartmentInfo department = Department.GetDepartment(userInfo.DepartmentID);
        if (userInfo.DepartmentID.Substring(0, 4) == "1300")
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" (SUBSTRING(DepartmentID, 5, 3) = '000') ");
            DepartmentList.DataBind();

        }
        else
        {
            DepartmentList.DataSource = Yipusoft.Account.BLL.Department.GetList(" DepartmentID= '" + department.DepartmentID.Substring(0, 4) + "000' ");
            DepartmentList.DataBind();

        }
    }

    void BindAll()
    {
        AllList.Items.Clear();
        List<DepartmentInfo> departments = Department.GetList(" (departmentid like '" + DepartmentList.SelectedValue.Substring(0, 4) + "___' and  departmentid not like '" + DepartmentList.SelectedValue.Substring(0, 4) + "0__') or departmentid='" + DepartmentList.SelectedValue.Substring(0, 4) + "000' ");
        for (int i = 0; i < departments.Count; i++)
        {
            //所有中队
            AllList.Items.Add(new ListItem(departments[i].ShortName, departments[i].DepartmentID));
        }
    }

    void BindSelect()
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;

        SelectList.Items.Clear();
        foreach (SlectDepartment selected in selectList)
        {
            SelectList.Items.Add(new ListItem(selected.text, selected.value));
        }

    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        selectList.Clear();
        ViewState["selectList"] = selectList;
        BindSelect();
    }
    protected void btnAllAdd_Click(object sender, EventArgs e)
    {
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (ListItem Item in AllList.Items)
        {
            int count = selectList.Where(or => or.value == Item.Value).Count();
            if (count == 0)
            {
                selectList.Add(new SlectDepartment() { text = Item.Text, value = Item.Value });
            }
        }
        ViewState["selectList"] = selectList;
        BindSelect();
    }

    protected void btnOk_Click(object sender, EventArgs e)
    {
        string Values = "";
        string Text = "";
        List<SlectDepartment> selectList = ViewState["selectList"] as List<SlectDepartment>;
        foreach (SlectDepartment selected in selectList)
        {
            Values += selected.value + ",";
            Text += selected.text + ",";
        }
        if (Text.Length != 0)
        {
            Text = Text.Substring(0, Text.Length - 1);
            Values = Values.Substring(0, Values.Length - 1);
        }
        string pp = Text + "*" + Values;
        string strScr = "<script language=javascript> window.returnValue='" + pp + "';  window.close(); </script>";
        Page.RegisterClientScriptBlock("Close", strScr);
    }

    protected void btnCannal_Click(object sender, EventArgs e)
    {
        string strScr = "<script language=javascript>   window.close(); </script>";
        Page.RegisterClientScriptBlock("Close", strScr);
    }

}
[Serializable]
public class SlectDepartment
{
    public string text;
    public string value;
}